package com.cdutetc.demo.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private int code;
    private boolean success;
    private String message;
    private T data;
    // 成功方法
    public static <T> Result<T> success(){
        return success(null);
    }

    public static <T> Result<T> success(T data){
        return success("操作成功", data);
    }

    public static <T> Result<T> success(String msg, T data){
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setSuccess(true);
        result.setMessage(msg);
        result.setData(data);
        return result;
    }

    // 失败方法 - 修复递归调用
    public static <T> Result<T> fail(){
        return fail("操作失败");
    }

    public static <T> Result<T> fail(String msg){
        return fail(400, msg, null);
    }

    public static <T> Result<T> fail(String msg, T data){
        return fail(400, msg, data);
    }

    public static <T> Result<T> fail(int code, String msg){
        return fail(code, msg, null);
    }

    public static <T> Result<T> fail(int code, String msg, T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setSuccess(false);
        result.setMessage(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(ResultCode code){
        return fail(code, null);
    }

    public static <T> Result<T> fail(ResultCode code, T data){
        Result<T> result = new Result<>();
        result.setCode(code.getCode());
        result.setSuccess(false);
        result.setMessage(code.getMessage());
        result.setData(data);
        return result;
    }
}